18. Database Implementation - Writing 2

Let’s implement message sending in the app.

To do so, we’ll add a click listener to the send button:

// Send button sends a message and clears the EditText
mSendButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
       // TODO: Send messages on click


       // Clear input box
       mMessageEditText.setText("");
   }
});

Within the onClick method, let’s create a FriendlyMessage object for the message that the user typed in. The FriendlyMessage object has three instance variables:
A String for the user’s name,
A String for the text of the message
A String for the URL of the photo if it’s a photo message.

In this case, we’re only sending text messages for now (we will implement photo-messaging later), so we’ll create a FriendlyMessage object with all the fields except for photoUrl, which will be null.

FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null);

This object has all the keys that we’ll store as a message in the realtime database. In the next step we’ll store this data to the cloud in our realtime database.